import type { NFTDetails } from '../../types.ts';

export const convertIpfsToHttp = (ipfsUri: string) => {
    if (ipfsUri.startsWith('ipfs://')) {
        return ipfsUri.replace(/^ipfs:\/\//, 'https://ipfs.io/ipfs/');
    }
    return ipfsUri;
};

export  const chunkArray = <T>(array: T[], chunkSize: number) => {
    const result = [];
    for (let i = 0; i < array.length; i += chunkSize) {
        result.push(array.slice(i, i + chunkSize));
    }
    return result;
};

export const readWishlist = (wishlistString: string) => {
    try {
        return JSON.parse(wishlistString)
            .map((el: Record<string, string>) => {
                const [nftAddr] = Object.keys(el);

                const tokenID = BigInt(Number(el[nftAddr]));

                return {
                    nftAddr,
                    tokenID,
                };
            }) as NFTDetails[];

    } catch {
        return [] as NFTDetails[];
    }
};